package cn.edu.scau.entity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import org.hibernate.annotations.Formula;

import com.fasterxml.jackson.annotation.JsonIgnore;


@Entity
@Table(name = "blog")
public class Blog {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "blog_id")
	private Integer blogId;

	@Formula("(select u.nickName from user u inner join user_blog ub where ub.blog_id = blog_id and u.user_id = ub.user_id)")
	private String user_name;

	@Formula("(select u.head_image from user u inner join user_blog ub where ub.blog_id = blog_id and u.user_id = ub.user_id)")
	private String headImage;

	@Column(name = "content", nullable = false, length = 512)
	// 考虑加上图片
	private String content;

	@Column(name = "upvote_num", nullable = false)
	private Integer upvoteNum = 0;

	@Column(name = "video_adress", length = 512)
	private String videoAdress;

	@Column(name = "time", nullable = false, columnDefinition = "datetime")
	private String time;

	@ElementCollection(targetClass = String.class, fetch = FetchType.EAGER)
	@CollectionTable(name = "blog_image", joinColumns = @JoinColumn(name = "blog_id", nullable = false) )
	@Column(name = "image_adress", nullable = false, length = 512)
	@OrderColumn(name = "list_order")
	private List<String> imageAdresses = new ArrayList<String>();
	
	@JsonIgnore
	@OneToMany(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
	@JoinTable(name = "blog_comment", joinColumns = @JoinColumn(name = "blog_id", referencedColumnName = "blog_id") , inverseJoinColumns = @JoinColumn(name = "comment_id", referencedColumnName = "comment_id", unique = true) )
	private Set<Comment> comments = new HashSet<Comment>();

	public Integer getBlogId() {
		return blogId;
	}

	public void setBlogId(Integer blogId) {
		this.blogId = blogId;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Integer getUpvoteNum() {
		return upvoteNum;
	}

	public void setUpvoteNum(Integer upvoteNum) {
		this.upvoteNum = upvoteNum;
	}

	public String getVideoAdress() {
		return videoAdress;
	}

	public void setVideoAdress(String videoAdress) {
		this.videoAdress = videoAdress;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public List<String> getImageAdresses() {
		return imageAdresses;
	}

	public void setImageAdresses(List<String> imageAdresses) {
		this.imageAdresses = imageAdresses;
	}

	public Set<Comment> getComments() {
		return comments;
	}

	public void setComments(Set<Comment> comments) {
		this.comments = comments;
	}

	public String getUser_name() {
		return user_name;
	}

	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

	public String getHeadImage() {
		return headImage;
	}

	public void setHeadImage(String headImage) {
		this.headImage = headImage;
	}

	@Override
	public String toString() {
		return "Blog [blogId=" + blogId + ", content=" + content + ", upvoteNum=" + upvoteNum + ", videoAdress="
				+ videoAdress + ", time=" + time + ", imageAdresses=" + imageAdresses + ", comments=" + comments
				+ "]";
	}
}
